package guiscreens;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.Date;
import java.sql.Time;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import dao.ExecuteQuery;

/**
 * This class handles the posting items panel.
 * 
 * @author Lee
 * @version 1.0
 */
public class NewItem extends JPanel {

	private CardLayout c1;
	private JPanel basePanel;
	private JFrame thisFrame;
	public static String[] catValues = {"Art", "Books", "Electronics", "Home And Garden", "Sporting Goods", "Toys", "Other" };
	public static String[] conValues  = {"New", "Very Good", "Good", "Fair", "Poor"};
	private String[] lengthValues;
	private JTextField nameField;
	private TextArea descriptionArea;
	private JComboBox catBox;
	private JComboBox conBox;
	private JTextField minField;
	private JTextField minSaleField;
	private JComboBox dateBox;
	private JTextField getNowField;
	private JCheckBox returnCheck;
	private boolean returns;

	/**
	 * Create the panel.
	 */
	public NewItem(final CardLayout c1, final JPanel basePanel, JFrame thisFrame)
	{
		this.c1 = c1;
		this.basePanel = basePanel;
		this.thisFrame = thisFrame;
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		setBackground(Color.WHITE);

		JPanel panel = new JPanel();
		panel.setBackground(Color.WHITE);
		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.anchor = GridBagConstraints.WEST;
		gbc_panel.insets = new Insets(0, 0, 5, 0);
		gbc_panel.fill = GridBagConstraints.VERTICAL;
		gbc_panel.gridx = 1;
		gbc_panel.gridy = 1;
		add(panel, gbc_panel);

		JLabel lblItemName = new JLabel("Item Name");
		panel.add(lblItemName);

		nameField = new JTextField();
		panel.add(nameField);
		nameField.setColumns(10);

		JPanel panel_8 = new JPanel();
		GridBagConstraints gbc_panel_8 = new GridBagConstraints();
		gbc_panel_8.anchor = GridBagConstraints.WEST;
		gbc_panel_8.insets = new Insets(0, 0, 5, 0);
		gbc_panel_8.fill = GridBagConstraints.VERTICAL;
		gbc_panel_8.gridx = 1;
		gbc_panel_8.gridy = 2;
		add(panel_8, gbc_panel_8);

		JLabel lblDescription = new JLabel("Description");
		panel_8.add(lblDescription);

		descriptionArea = new TextArea();
		panel_8.add(descriptionArea);
		panel_8.setBackground(Color.WHITE);

		JPanel panel_1 = new JPanel();
		GridBagConstraints gbc_panel_1 = new GridBagConstraints();
		gbc_panel_1.anchor = GridBagConstraints.WEST;
		gbc_panel_1.insets = new Insets(0, 0, 5, 0);
		gbc_panel_1.fill = GridBagConstraints.VERTICAL;
		gbc_panel_1.gridx = 1;
		gbc_panel_1.gridy = 3;
		add(panel_1, gbc_panel_1);

		JLabel lblCategory = new JLabel("Category");
		panel_1.add(lblCategory);
		panel_1.setBackground(Color.WHITE);

		catBox = new JComboBox(catValues);
		panel_1.add(catBox);

		JPanel panel_2 = new JPanel();
		panel_2.setBackground(Color.WHITE);
		GridBagConstraints gbc_panel_2 = new GridBagConstraints();
		gbc_panel_2.anchor = GridBagConstraints.WEST;
		gbc_panel_2.insets = new Insets(0, 0, 5, 0);
		gbc_panel_2.fill = GridBagConstraints.VERTICAL;
		gbc_panel_2.gridx = 1;
		gbc_panel_2.gridy = 4;
		add(panel_2, gbc_panel_2);

		JLabel lblCondition = new JLabel("Condition");
		panel_2.add(lblCondition);

		conBox = new JComboBox(conValues);
		panel_2.add(conBox);

		JPanel panel_3 = new JPanel();
		panel_3.setBackground(Color.WHITE);
		GridBagConstraints gbc_panel_3 = new GridBagConstraints();
		gbc_panel_3.anchor = GridBagConstraints.WEST;
		gbc_panel_3.insets = new Insets(0, 0, 5, 0);
		gbc_panel_3.fill = GridBagConstraints.VERTICAL;
		gbc_panel_3.gridx = 1;
		gbc_panel_3.gridy = 5;
		add(panel_3, gbc_panel_3);

		JLabel lblStartAuctionBidding = new JLabel("Start auction bidding at $");
		panel_3.add(lblStartAuctionBidding);

		minField = new JTextField();
		panel_3.add(minField);
		minField.setColumns(10);

		JPanel panel_4 = new JPanel();
		panel_4.setBackground(Color.WHITE);
		GridBagConstraints gbc_panel_4 = new GridBagConstraints();
		gbc_panel_4.anchor = GridBagConstraints.WEST;
		gbc_panel_4.insets = new Insets(0, 0, 5, 0);
		gbc_panel_4.fill = GridBagConstraints.VERTICAL;
		gbc_panel_4.gridx = 1;
		gbc_panel_4.gridy = 6;
		add(panel_4, gbc_panel_4);

		JLabel lblMinimumSalePrice = new JLabel("Minimum sale price $");
		panel_4.add(lblMinimumSalePrice);

		minSaleField = new JTextField();
		panel_4.add(minSaleField);
		minSaleField.setColumns(10);

		JPanel panel_5 = new JPanel();
		panel_5.setBackground(Color.WHITE);
		GridBagConstraints gbc_panel_5 = new GridBagConstraints();
		gbc_panel_5.anchor = GridBagConstraints.WEST;
		gbc_panel_5.insets = new Insets(0, 0, 5, 0);
		gbc_panel_5.fill = GridBagConstraints.VERTICAL;
		gbc_panel_5.gridx = 1;
		gbc_panel_5.gridy = 7;
		add(panel_5, gbc_panel_5);

		JLabel lblAuctionEndsIn = new JLabel("Auction ends in ");
		panel_5.add(lblAuctionEndsIn);

		lengthValues = new String[4];
		lengthValues[0] = "1 days";
		lengthValues[1] = "3 days";
		lengthValues[2] = "5 days";
		lengthValues[3] = "7 days";
		dateBox = new JComboBox(lengthValues);
		panel_5.add(dateBox);

		JPanel panel_6 = new JPanel();
		panel_6.setBackground(Color.WHITE);
		GridBagConstraints gbc_panel_6 = new GridBagConstraints();
		gbc_panel_6.anchor = GridBagConstraints.WEST;
		gbc_panel_6.insets = new Insets(0, 0, 5, 0);
		gbc_panel_6.fill = GridBagConstraints.VERTICAL;
		gbc_panel_6.gridx = 1;
		gbc_panel_6.gridy = 8;
		add(panel_6, gbc_panel_6);

		JLabel lblGetItNow = new JLabel("Get It Now price $");
		panel_6.add(lblGetItNow);

		getNowField = new JTextField();
		panel_6.add(getNowField);
		getNowField.setColumns(10);

		JPanel panel_7 = new JPanel();
		panel_7.setBackground(Color.WHITE);
		GridBagConstraints gbc_panel_7 = new GridBagConstraints();
		gbc_panel_7.anchor = GridBagConstraints.WEST;
		gbc_panel_7.insets = new Insets(0, 0, 5, 0);
		gbc_panel_7.fill = GridBagConstraints.VERTICAL;
		gbc_panel_7.gridx = 1;
		gbc_panel_7.gridy = 9;
		add(panel_7, gbc_panel_7);

		JLabel lblReturnsAccepted = new JLabel("Returns Accepted?");
		panel_7.add(lblReturnsAccepted);

		returnCheck = new JCheckBox();
		returnCheck.addItemListener(new ItemListener(){
			public void itemStateChanged(ItemEvent event)
			{
				returns = returnCheck.isSelected();
			}
		});
		panel_7.add(returnCheck);

		JPanel panel_9 = new JPanel();
		panel_9.setBackground(Color.WHITE);
		GridBagConstraints gbc_panel_9 = new GridBagConstraints();
		gbc_panel_9.anchor = GridBagConstraints.EAST;
		gbc_panel_9.fill = GridBagConstraints.VERTICAL;
		gbc_panel_9.gridx = 1;
		gbc_panel_9.gridy = 10;
		add(panel_9, gbc_panel_9);

		JButton btnCancel = new JButton("Cancel");
		panel_9.add(btnCancel);
		btnCancel.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				c1.first(basePanel);
			}
		});

		JButton btnListMyItem = new JButton("List My Item");
		btnListMyItem.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				if (!(nameField.getText().isEmpty() || descriptionArea.getText().isEmpty() || 
						minField.getText().isEmpty() || minSaleField.getText().isEmpty()))
				{
					String name = nameField.getText();
					String description = descriptionArea.getText();
					String category = catValues[catBox.getSelectedIndex()];
					String conditionRaw = conValues[conBox.getSelectedIndex()];

					int condition = 0;
					if (conditionRaw.equals("Fair")) condition = 1;
					if (conditionRaw.equals("Good")) condition = 2;
					if (conditionRaw.equals("Very Good")) condition = 3;
					if (conditionRaw.equals("New")) condition = 4;
					try 
					{
						double startBid = Double.parseDouble(minField.getText());
						double minPrice = Double.parseDouble(minSaleField.getText());

						String dayRaw = lengthValues[dateBox.getSelectedIndex()];
						int dateAdder = 0;
						if (dayRaw.equals("1 days")) dateAdder = 1;
						if (dayRaw.equals("3 days")) dateAdder = 3;
						if (dayRaw.equals("5 days")) dateAdder = 5;
						if (dayRaw.equals("7 days")) dateAdder = 7;

						Date date = new Date(System.currentTimeMillis() + (dateAdder * 86400000L));
						Time time = new Time(System.currentTimeMillis());
						String getNow = "";
						if (!getNowField.getText().equals("")) {
							getNow = getNowField.getText();
						}

						try 
						{
							if (Double.parseDouble(getNow) > minPrice)
							{
								ExecuteQuery.postItem(name, description, category, condition,
										startBid, minPrice, date, Double.parseDouble(getNow), returns, login.username, time);
								JOptionPane.showMessageDialog(null, "Item Listed!");
							}
							else JOptionPane.showMessageDialog(null, "GetItNowPrice should be greater or equal to the min sale price");
						} catch (NumberFormatException e) {
							ExecuteQuery.postItem(name, description, category, condition, startBid, minPrice, date, null, returns, login.username, time);
							JOptionPane.showMessageDialog(null, "Item Listed without a get it now price");
						}
					} catch(NumberFormatException e) { JOptionPane.showMessageDialog(null, "Please enter a monetary amount");}
				}
			else JOptionPane.showMessageDialog(null, "Please enter all fields (except getitnow, which is optional)");
		}
	});
		panel_9.add(btnListMyItem);
		thisFrame.setTitle("New item for Auction");

}

}
